CochoApp = function () {//构造函数
    this.storageTokenKey = "token";
    this.storageUserInfoKey = "storageUserInfoKey";
    //this.baseUrl = "/yunCashierDf";
    this.baseUrl = "/yunCashierDf";
    this.token = "";//记录当前用户的token
    this.userInfo;//记录当前用户的用户信息
    this.storageMenuTableIdKey = "storageMenuTableIdKey";
    this.storageMenuListInfosKey = "storageMenuListInfosKey";

    this.SUCCESS = "SUCCESS";
    this.FAIL = "FAIL";
};

CochoApp.prototype = { //定义方法

    setTokenToStorage: function (tokenValue) {
        this.token = tokenValue;
        cochoStorage.setStorage(this.storageTokenKey, encodeURI(tokenValue));
    },

    getTokenByStorage: function () {
        if (this.isEmpty(this.token)) {
            var tokenValue = cochoStorage.getStorage(this.storageTokenKey);
            if (this.isNotEmpty(tokenValue)) {
                this.token = decodeURI(tokenValue);
            }
        }
        return this.token;
    },

    setUserInfoToStorage: function (userInfoJson) {
        if (this.isNotEmpty(userInfoJson)) {
//            var userInfoJson = JSON.stringify(userInfo);
            cochoStorage.setStorage(this.storageUserInfoKey, encodeURI(userInfoJson));
            this.userInfo = userInfoJson;
        }
    },

    getUserInfoByStorage: function () {
        if (this.isEmpty(this.userInfo)) {
            var userInfoJsonURI = cochoStorage.getStorage(this.storageUserInfoKey);
            if (this.isNotEmpty(userInfoJsonURI)) {
                var userInfoJson = decodeURI(userInfoJsonURI);
                this.userInfo = JSON.parse(userInfoJson);
            }
        }
        return this.userInfo;
    },

    isEmpty: function (value) {
        return value == null || value == "null" || value == undefined || value == "undefined" || value.length < 1;
    },

    isNotEmpty: function (value) {
        return !this.isEmpty(value);
    },

    /**
     * 是否被拦截器拦截到
     * @param statusCode   状态码
     * @returns {boolean}  true：拦截到
     */
    isInterceptored: function (statusCode) {
        return statusCode.indexOf("INTERCEPTORED") > -1;
    },


    /**
     * 更换验证码
     * @param codeKeyId 验证码键所在标签的ID
     * @param codeImgId 验证码图片所在标签的ID
     */
    changeVerifyCode: function (codeKeyId, codeImgId) {
        var codeKey = $(codeKeyId).val();
        var paramData = {"codeKey": codeKey};
        $.ajax({
            type: "post",
            async: true,//异步，如果等于false 那么就是同步
            url: cochoApp.baseUrl + "/anonymous/cache/getVerifyCode.do",
            dataType: "json",
            data: paramData,
            success: function (data) {
                if (data != null) {
                    $(codeImgId).attr('src', data.imgUrl);
                    $(codeKeyId).val(data.codeKey);
                }
            },
            error: function (data) {
            }
        });
    },

    /**
     * 检查验证码是否正确，true:正确，false：错误
     * @param codeKeyId 验证码键所在标签的ID
     * @param codeContentId 验证码值所在标签的ID
     * @param codeImgId 验证码图片所在标签的ID
     */
    checkVerifyCode: function (codeKeyId, codeContentId, codeImgId) {
        var result = "FAIL";
        var codeKey = $(codeKeyId).val();
        var codeContent = $(codeContentId).val();
        var paramData = {"codeKey": codeKey, "codeContent": codeContent};
        $.ajax({
            type: "post",
            async: false,//异步，如果等于false 那么就是同步
            url: cochoApp.baseUrl + "/anonymous/cache/checkVerifyCode.do",
            dataType: "json",
            data: paramData,
            success: function (data) {
                if (data != null && data == true) {
                    result = "SUCCESS";
                }
            },
            error: function (data) {
            }
        });
        this.changeVerifyCode(codeKeyId, codeImgId);
        return result;
    }


};

cochoApp = new CochoApp();


Date.prototype.format = function (format) {
    var o = {
        "M+": this.getMonth() + 1,
        "d+": this.getDate(),
        "h+": this.getHours(),
        "m+": this.getMinutes(),
        "s+": this.getSeconds(),
        "q+": Math.floor((this.getMonth() + 3) / 3),
        "S": this.getMilliseconds()
    };
    if (/(y+)/.test(format)) {
        format = format.replace(RegExp.$1, (this.getFullYear() + "").substr(4 - RegExp.$1.length));
    }
    for (var k in o) {
        if (new RegExp("(" + k + ")").test(format)) {
            format = format.replace(RegExp.$1, RegExp.$1.length == 1 ? o[k] : ("00" + o[k]).substr(("" + o[k]).length));
        }
    }
    return format;
}
